package com.taotao.item.freemarker;

import java.io.File;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import freemarker.template.Configuration;
import freemarker.template.Template;

/**
 * 使用freemarker模板生成工具，通过activeMQ消息队列，能实现在对商品进行操作时立马生成相对应的商品详情页面
 * @author created by wq
 * @date   Sep 5, 2017---11:36:21 AM
 * @return
 * @throws Exception<br/>
 *==========History========== <br/>
 * taotao-item-web Sep 5, 2017
 */
@Controller
public class HtmlFreemarkerController {

	@Autowired
	private FreeMarkerConfigurer freeMarkerConfigurer;
	
	@RequestMapping("/genHtml")
	@ResponseBody
	public String createStaticHtml() throws Exception{
		Configuration configuration = freeMarkerConfigurer.getConfiguration();
		Template template = configuration.getTemplate("hello.ftl");
		//创建数据集
		Map map = new HashMap<>();
		map.put("hello", "freemarker 与 spring 的整合。。。");
		//创建FileWriter
		FileWriter out = new FileWriter(new File("D:\\freemarker_html\\out.html"));
		//调用process方法生成文件[参数一为 数据集 ； 参数而为生成文件的 输出流对象FileWriter]
		template.process(map, out);
		out.close();
		return "ok";
	}
	
	
	
}